﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using System.Data.Entity;
using Equin.ApplicationFramework;
using Library;

namespace LabManager.UserControls.Tabs.Products
{
    public partial class TabProductType : TabTemplate
    {
        private BindingListView<V_ProductType> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabProductType()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadManufacturer();
            LoadProductCategory();
            LoadProductTypeList();
        }

        private void SetTexts()
        {
            //TODO
        }

        private void LoadManufacturer()
        {
            var manufacturer = new BindingListView<Manufacturer>(DataConnectorLayer.Instance.R_Manufacturer.FindAll(x => x.ManufacturerID > 0).ToList<Manufacturer>());

            cbManufacturer.DataSource = manufacturer;
            cbManufacturer.ValueMember = "ManufacturerID";
            cbManufacturer.DisplayMember = "ManufacturerName";
        }

        private void LoadProductCategory()
        {
            var productCategory = new BindingListView<ProductCategory>(DataConnectorLayer.Instance.R_ProductCategory.FindAll(x => x.ProductCategoryID > 0).ToList<ProductCategory>());

            cbProductCategory.DataSource = productCategory;
            cbProductCategory.ValueMember = "ProductCategoryID";
            cbProductCategory.DisplayMember = "ProductCategoryName";   
        }

        private void LoadProductTypeList()
        {
            // Get new entities from view
            bindingList = new BindingListView<V_ProductType>(DataConnectorLayer.Instance.R_V_ProductType.FindAll(x => x.ProductTypeID > 0).ToList<V_ProductType>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                cbManufacturer.Text = selectedRow.Cells[1].Value.ToString();
                cbProductCategory.Text = selectedRow.Cells[2].Value.ToString();
                tbRef.Text = selectedRow.Cells[3].Value.ToString();
                tbInventoryCode.Text = selectedRow.Cells[4].Value.ToString();
                tbTag.Text = selectedRow.Cells[5].Value.ToString();
                tbProductDescription.Text = selectedRow.Cells[6].Value.ToString();

                // Delete button
                var canDeleteProduct = DataConnectorLayer.Instance.R_Product.FindOne(x => x.ProductTypeID == selectedID) == null;
                var canDeleteLabEnvironmentProductType = DataConnectorLayer.Instance.R_LabEnvironmentProductType.FindOne(x => x.LabEnviromentProductTypeID == selectedID) == null;
                var canDeleteProductMachineCompatibility = DataConnectorLayer.Instance.R_ProductMachineCompatibility.FindOne(x => x.ProductTypeID == selectedID) == null;

                btDelete.Enabled = canDeleteProduct && canDeleteLabEnvironmentProductType && canDeleteProductMachineCompatibility;

                // Update
                btUpdate.Enabled = true;
            }
            else
            {
                tbRef.Text = string.Empty;
                tbInventoryCode.Text = string.Empty;
                tbTag.Text = string.Empty;
                tbProductDescription.Text = string.Empty;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }

            // New button
            var canCreateManufacturer = DataConnectorLayer.Instance.R_Manufacturer.Set<Manufacturer>().Count() > 0;
            var canCreateProductCategory = DataConnectorLayer.Instance.R_ProductCategory.Set<ProductCategory>().Count() > 0;
            btNew.Enabled = canCreateManufacturer && canCreateProductCategory;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var manufacturerID = DataConnectorLayer.Instance.R_Manufacturer.FindOne().ManufacturerID;
            var productCategoryID = DataConnectorLayer.Instance.R_ProductCategory.FindOne().ProductCategoryID;

            var item = new ProductType() { ManufacturerID = manufacturerID, ProductCategoryID = productCategoryID, REF = Helper.EMPTY_STRING, InventoryCode = Helper.EMPTY_STRING, Tag = Helper.EMPTY_STRING, ProductDescription = Helper.EMPTY_STRING };
            DataConnectorLayer.Instance.R_ProductType.Create(item);
            DataConnectorLayer.Instance.R_ProductType.SaveChanges();
            LoadProductTypeList();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_ProductType.FindById(selectedID);
            DataConnectorLayer.Instance.R_ProductType.Delete(item);
            DataConnectorLayer.Instance.R_ProductType.SaveChanges();

            LoadProductTypeList();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_ProductType.FindById(selectedID);

            item.ManufacturerID = (int)cbManufacturer.SelectedValue;
            item.ProductCategoryID = (int)cbProductCategory.SelectedValue;
            item.REF = tbRef.Text;
            item.InventoryCode = tbInventoryCode.Text;
            item.Tag = tbTag.Text;
            item.ProductDescription = tbProductDescription.Text;

            DataConnectorLayer.Instance.R_ProductType.Update(item);
            DataConnectorLayer.Instance.R_ProductType.SaveChanges();

            LoadProductTypeList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(V_ProductType productType)
            {
                return productType.ManufacturerName.ToLower().Contains(search) ||
                        productType.ProductCategoryName.ToLower().Contains(search) ||
                        productType.REF.ToLower().Contains(search) ||
                        productType.InventoryCode.ToLower().Contains(search) ||
                        productType.Tag.ToLower().Contains(search) ||
                        productType.ProductDescription.ToLower().Contains(search);
            });
        }

        #endregion

    }
}
